package com.isoft.filelistener.RunnerForFiles;

import com.isoft.filelistener.flistener.FListener;
import com.isoft.filelistener.flistener.FileListener;
import com.isoft.filelistener.flistener.FileListenerf;
import com.isoft.filelistener.flistener.FileListeners;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
@Slf4j
public class ListenerRunnerForFiles implements ApplicationRunner {
    private String filePath="D:\\files";
    @Override
    public void run(ApplicationArguments args) throws Exception {
        try{
            //one
//            FileAlterationObserver fileAlterationObserver=new FileAlterationObserver(new File(filePath));
//            FListener fListener=new FListener();
//            fileAlterationObserver.addListener(fListener);
//           //two
//            FileAlterationObserver fileAlterationObserver2=new FileAlterationObserver(new File(filePath));
//            FileListener fileListener = new FileListener();
//            fileAlterationObserver2.addListener(fileListener);
//            //three
//             FileAlterationObserver fileAlterationObserver3=new FileAlterationObserver(new File(filePath));
//            FileListeners fileListeners = new FileListeners();
//            fileAlterationObserver3.addListener(fileListeners);
            //four
            FileAlterationObserver fileAlterationObserver=new FileAlterationObserver(new File(filePath));
            FileListenerf fileListenerf= new FileListenerf();
            fileAlterationObserver.addListener(fileListenerf);

            FileAlterationMonitor fileAlterationMonitor=new FileAlterationMonitor(3000,new FileAlterationObserver[]{fileAlterationObserver});
            fileAlterationMonitor.start();
        }catch (Exception ex){
            log.info("文件监听启动失败");
            ex.printStackTrace();
        }
    }
}
